package com.komorebishao.code;

import java.util.Arrays;

public class Code26 {

    public static void main(String... args) {
        int[] input = new int[] {1, 1, 2};
        int resultLength = getResultIndex(input);
        int[] result = Arrays.copyOf(input, resultLength);
        System.out.println("resultLength: "  + resultLength);
        System.out.println(Arrays.toString(result));
    }

    public static int getResultIndex(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int slowIndex = 0;
        int fastIndex = 1;
        while (fastIndex < nums.length) {
            if (nums[fastIndex] != nums[slowIndex]) {
                nums[slowIndex + 1] = nums[fastIndex];
                slowIndex ++;
            }
            fastIndex ++;
        }
        return slowIndex + 1;
    }

    public static int getResultIndex2(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int slowIndex = 0;
        int fastIndex = 1;
        while (fastIndex < nums.length) {
            if (nums[fastIndex] != nums[slowIndex]) {
                if (fastIndex - slowIndex > 1) {
                    nums[slowIndex + 1] = nums[fastIndex];
                }
                slowIndex ++;
            }
            fastIndex ++;
        }
        return slowIndex + 1;
    }

}
